package com.hulk.ratel.web.control;


import com.hulk.ratel.common.constant.enums.BussErrCode;
import com.hulk.ratel.common.exception.BaseRteException;
import com.hulk.ratel.common.exception.ServiceRteException;
import com.hulk.ratel.common.exception.WebRteException;
import com.hulk.ratel.component.R;
import com.hulk.ratel.service.InitiativeScanPayService;

import com.hulk.ratel.service.dto.InitiativeScanPayReqDTO;
import com.hulk.ratel.service.dto.InitiativeScanPayRespDTO;
import io.swagger.annotations.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;


/**
 * Created by hulk
 */
@Api(basePath = "/v1/api", value="OrderPayController",description="订单支付类",tags={"订单支付类","URI:/v1/api"})
@Slf4j
@RestController
@RequestMapping(value = "v1/api")
public class InitiativeScanPayController extends AbsBaseCtl {

    @Autowired
    private InitiativeScanPayService payService;

    //交易代码
    @ApiOperation(value = "excute", notes = "订单支付接口", tags = {"订单支付接口", "URI:/INITIATIVE_SCANPAY"}, httpMethod = "POST",
            response = String.class, produces = MediaType.APPLICATION_JSON_UTF8_VALUE, consumes = MediaType.APPLICATION_FORM_URLENCODED_VALUE)
    @ApiImplicitParams({
            @ApiImplicitParam(dataType = "String", name = "partnerNo", value = "合作方编号", required = true),
            @ApiImplicitParam(dataType = "String", name = "traceId", value = "合作方交易流水号", required = true),
            @ApiImplicitParam(dataType = "String", name = "plainText", value = "报文", required = true),
            @ApiImplicitParam(dataType = "String", name = "partnerAESKey", value = "AES秘钥", required = true),
    })
    @ApiResponses(value = {
            @ApiResponse(code = 200, message = "成功"),
            @ApiResponse(code = 500, message = "处理错误")})
    @PostMapping(value = "/INITIATIVE_SCANPAY", produces = {MediaType.APPLICATION_JSON_UTF8_VALUE}, consumes = {MediaType.APPLICATION_FORM_URLENCODED_VALUE})
    @ResponseBody
    @Override
    public String excute(String partnerNo, String traceId, String plainText, String partnerAESKey,  String intCutDate , String workId) {

        log.info("workId:{} 扫码支付<主扫> 请求参数：partnerNo:{},traceId:{},plainText:{},", workId, partnerNo, traceId, plainText);
        InitiativeScanPayReqDTO reqDTO = new InitiativeScanPayReqDTO();
        InitiativeScanPayRespDTO respDTO = new InitiativeScanPayRespDTO();
        try {
             reqDTO = this.preHandle(plainText, workId, null, InitiativeScanPayReqDTO.class);
             respDTO = new InitiativeScanPayRespDTO();
            //super.checkValidateReq(workId,reqDTO);
            R<InitiativeScanPayRespDTO> resp = payService.excute(reqDTO,intCutDate,workId);

            if (resp.isSuccess()) {
                respDTO = resp.getResult();
            } else {
                throw new WebRteException(workId,resp.getErrCode(),resp.getErrMsg());
            }

        } catch (BaseRteException ex) {
            log.error("platformId:{} 扫码支付<主扫>失败！{}{}", workId, ex.getErrorCode(), ex.getErrorMessage());
            respDTO = this.initRespDTO(reqDTO, workId, ex.getErrorCode(), ex.getErrorMessage(), null);
        } catch (Exception e) {
            log.error("platformId:{} 扫码支付<主扫>失败！{}", workId, e);
            respDTO = this.initRespDTO(reqDTO, workId, BussErrCode.E_900002.getErrCode(), BussErrCode.E_900002.getErrDesc(), null);
        }

        return this.postHandle(respDTO, partnerAESKey, partnerNo, workId);
    }

    /**
     * 初始化响应参数
     **/

    private InitiativeScanPayRespDTO initRespDTO(InitiativeScanPayReqDTO reqDto,
                                                 String workId,
                                                 String errCode,
                                                 String errMsg,
                                                 String qrCode) {
        InitiativeScanPayRespDTO respDto = (InitiativeScanPayRespDTO)initRespDTO(reqDto, workId, errCode, errMsg);
        respDto.setQrCode(qrCode);
        return respDto;
    }


}